import {
  NavigationGuardNext,
  createRouter,
  createWebHistory,
  RouteLocationNormalized,
  RouteRecordRaw,
} from "vue-router";
import { notification } from 'ant-design-vue';
// import routes from "pages-generated";

// 路由配置 和以前vue2一样
const routes: RouteRecordRaw[] = [
  {
    path: "/",
    name: "菜单页",
    meta: {
      title: "antdesgin",
    },
    component: () => import("../pages/antdesgin"),
    children:[
      {
        path:"/Main",
        name:"sub1",
        meta:{
          title:"Main",
        },
        component: () => import("../pages/Main/Main"),
      },
      {
        path:"/SetUp",
        name:"sub2",
        meta:{
          title:"SetUp",
        },
        component: () => import("../pages/SetUp/SetUp"),
      },
      {
        path:"/Edit",
        name:"sub3",
        meta:{
          title:"Edit",
        },
        component: () => import("../pages/Edit/Edit"),
      },
      {
        path:"/Tabs",
        name:"sub5",
        meta:{
          title:"Tabs",
        },
        component: () => import("../pages/Tabs/Tabs"),
      },
      {
        path:"/Button",
        name:"sub33",
        meta:{
          title:"Button",
        },
        component: () => import("../pages/Button/Button"),
      }
    ]
  },
  {
    path: "/register",
    name: "注册",
    meta: {
      title: "register",
    },
    component: () => import("../pages/register/register"),
  },
  {
    path: "/login",
    name: "登陆",
    meta: {
      title: "login",
    },
    component: () => import("../pages/login/login"),
  },
  {
    path: "/xiu",
    name: "xiu",
    meta: {
      title: "xiu",
    },
    component: () => import("../pages/xiu"),
  },
];

const router = createRouter({
  history: createWebHistory(),
  routes,
});

// 路由守卫和之前的实现方式一致 此处只是做了一个demo仅供演示
router.beforeEach(
  (
    to: RouteLocationNormalized,
    from: RouteLocationNormalized,
    next: NavigationGuardNext
  ) => {
    // 获取userToken，根据业务场景可由localStorage也可由cookie中获取
    const token = sessionStorage.getItem("token");
    // 路由守卫判断
    if (to.path === "/login" && token) {
      next("/");
      return;
    }
    if (to.path !== "/login" && !token) {
      // message.error("This is an error message");
        notification["error"]({
          message: "你需要先登陆!",
        });
      next("/login");
      return;
    }
    next();
  }
);



export { routes };
export default router;
